package com.bsj.power.system.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.extra.servlet.ServletUtil;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.dto.PageDTO;
import com.bsj.power.common.def.vo.sys.RoleVO;
import com.bsj.power.common.def.vo.web.system.role.RoleAddVO;
import com.bsj.power.common.util.IpUtils;
import com.bsj.power.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 角色模块
 * @date 2024/4/24
 */
@RestController
@RequestMapping("/system/role")
//@SaCheckPermission("/master/system/role")
public class SystemRoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色新增/修改
     *
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/addAndUpdate")
    public JsonResult<Long> addAndUpdate(HttpServletRequest request, @Validated @RequestBody RoleAddVO vo) {
        return JsonResult.success(roleService.addAndUpdate(vo, IpUtils.getIp(request)));
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("/delete")
    public JsonResult<Integer> delete(HttpServletRequest request, @RequestParam @NotNull(message = "roleId不能为空") Long roleId) {
        return JsonResult.success(roleService.delete(roleId, IpUtils.getIp(request)));
    }

    /**
     * 获取角色集合
     *
     * @return
     */
    @GetMapping("/doRolePageList")
    public JsonResult<List<RoleVO>> doRolePageList(@Valid PageDTO dto, Integer roleType) {
        return roleService.rolePageList(dto, roleType);
    }

//    /**
//     * 获取系统权限集合
//     *
//     * @return
//     */
//    @GetMapping("/doPermissionList")
//    public JsonResult<List<Permission>> doPermissionList() {
//        List<Permission> list = new ArrayList<>();
//        Map<String, Object> value = EnumUtil.getNameFieldMap(SysModulePermissionEnum.class, "desc");
//        value.entrySet().forEach(item -> {
//            String key = item.getKey();
//            String va = item.getValue().toString();
//            Permission permission = new Permission();
//            permission.setKey(key);
//            permission.setDesc(va);
//            list.add(permission);
//        });
//        return JsonResult.success(list);
//    }
}
